package com.gitee.zycra.future.job.web.controller;

import com.gitee.zycra.future.job.common.bo.CommonResult;
import com.gitee.zycra.future.job.common.bo.PageQuery;
import com.gitee.zycra.future.job.common.config.EncryptConfig;
import com.gitee.zycra.future.job.common.param.AddUserParam;
import com.gitee.zycra.future.job.common.param.EncryptParam;
import com.gitee.zycra.future.job.common.param.PageQueryUserParam;
import com.gitee.zycra.future.job.common.param.UpdateUserParam;
import com.gitee.zycra.future.job.common.param.UserLoginParam;
import com.gitee.zycra.future.job.common.util.JSONUtil;
import com.gitee.zycra.future.job.common.util.RSAUtil;
import com.gitee.zycra.future.job.common.vo.JobUserVO;
import com.gitee.zycra.future.job.common.vo.UserLoginVO;
import com.gitee.zycra.future.job.service.JobUserService;
import com.gitee.zycra.future.job.web.annotation.AuthCheck;
import com.gitee.zycra.jdbc.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zycra
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class JobUserController {

    @Autowired
    private JobUserService jobUserService;

    @Autowired
    private EncryptConfig encryptConfig;

    /**
     * 添加
     *
     * @param param 用户对象JSON加密
     * @return 添加结果
     */
    @AuthCheck({"0"})
    @PostMapping("/addUser")
    public CommonResult<String> addUser(@RequestBody EncryptParam param) {
        String json = RSAUtil.decryptByPrivateKey(param.getParam(), encryptConfig.getPrivateKey());
        AddUserParam addUserParam = JSONUtil.parseObject(json, AddUserParam.class);
        if (addUserParam == null || addUserParam.getUsername() == null || addUserParam.getPwd() == null || addUserParam.getAuth() == null) {
            return CommonResult.failed();
        }
        jobUserService.insertJobUser(addUserParam);
        return CommonResult.success();
    }

    /**
     * 禁用/启用
     *
     * @param id      用户ID
     * @param enabled 是否启用
     * @return 影响条数
     */
    @AuthCheck({"0"})
    @PostMapping("/changeUser")
    public CommonResult<String> changeUser(@RequestParam("id") Long id, @RequestParam("enabled") Boolean enabled) {
        jobUserService.changeJobUser(id, enabled);
        return CommonResult.success();
    }

    /**
     * 更新
     *
     * @param param 用户对象
     * @return 更新结果
     */
    @AuthCheck({"0"})
    @PostMapping("/updateUser")
    public CommonResult<String> updateUser(@RequestBody @Validated UpdateUserParam param) {
        jobUserService.updateJobUser(param);
        return CommonResult.success();
    }

    /**
     * 分页查询
     *
     * @param pageQuery 查询参数
     * @return 查询结果
     */
    @AuthCheck({"0"})
    @PostMapping("/pageList")
    public CommonResult<PageResult<JobUserVO>> pageList(@RequestBody @Validated PageQuery<PageQueryUserParam> pageQuery) {
        PageResult<JobUserVO> pageResult = jobUserService.selectJobUserForPage(pageQuery);
        return CommonResult.success(pageResult);
    }

    /**
     * 获取加密用公钥
     *
     * @return 公钥
     */
    @GetMapping("/getPublicKey")
    public CommonResult<String> getPublicKey() {
        return CommonResult.success(RSAUtil.convertToFormatKey(encryptConfig.getPublicKey(), false));
    }

    /**
     * 登录
     *
     * @param param 登录对象JSON加密
     * @return token
     */
    @PostMapping("/login")
    public CommonResult<UserLoginVO> login(@RequestBody EncryptParam param) {
        String json = RSAUtil.decryptByPrivateKey(param.getParam(), encryptConfig.getPrivateKey());
        if (json == null) {
            return CommonResult.failed("参数错误");
        }
        UserLoginParam userLoginParam = JSONUtil.parseObject(json, UserLoginParam.class);
        return jobUserService.login(userLoginParam);
    }

    /**
     * 获取验证码
     *
     * @return token
     */
    @GetMapping("/getCaptcha")
    public CommonResult<UserLoginVO> getCaptcha() {
        return jobUserService.getCaptcha();
    }
}
